require 'fileutils'
require 'qiniu'
require 'digest'
require 'openssl'
require 'base64'
require 'time'
require 'net/http'
require 'uri'
class KnowledgeApi < Grape::API
	WillPaginate.per_page = 20
	version 'v1', using: :path
	format :json
	default_format :json
	formatter :json, Grape::Formatter::Rabl
	resource :knowledge do
		#### 查知识类目 ####
		desc '查知识类目'
		params do
			optional :user_id, type: String, desc: '用户名'
			optional :parent_id, type: String, desc: '上级ID'
			optional :page, type: String, desc: '页码'
		end
		get 'knowledge_categories' do
			if params[:user_id].nil? 
				{code: "400", msg: "参数错误"}
			else
				if params[:page].nil?
					page = 1
				else
					page = params[:page]
				end
				if params[:parent_id].nil?
					knowledge_categories = KnowledgeCategory.where(parent_id: 0).paginate(:page => page).order('created_at DESC')
					knowledge_category_list = []
					knowledge_categories.each do |kc|
						knowledge_category = Hash.new
						knowledge_category["id"] = kc.id
						knowledge_category["category_img"] = kc.category_img
						knowledge_category["category_name"] = kc.category_name
						knowledge_category["category_type"] = kc.category_type
						knowledge_category_list << knowledge_category
					end
					count = knowledge_categories.size
					next_page = params[:page].to_i + 1
					{
						code: "0", 
						msg: "OK",
						knowledge_category_list: knowledge_category_list,
						count: count,
						next_page: next_page
					}
				else
					knowledge_categories = KnowledgeCategory.where(parent_id: params[:parent_id]).paginate(:page => page).order('created_at DESC')
					knowledge_category_list = []
					knowledge_categories.each do |kc|
						knowledge_category = Hash.new
						knowledge_category["id"] = kc.id
						knowledge_category["category_img"] = kc.category_img
						knowledge_category["category_name"] = kc.category_name
						knowledge_category["category_type"] = kc.category_type
						knowledge_category_list << knowledge_category
					end
					count = knowledge_categories.size
					next_page = params[:page].to_i + 1
					{
						code: "0", 
						msg: "OK",
						knowledge_category_list: knowledge_category_list,
						count: count,
						next_page: next_page
					}
				end
			end
		end
		#### 查知识类目 ####
		desc '查知识条目'
		params do
			optional :user_id, type: String, desc: '用户名'
			optional :category_id, type: String, desc: '上级ID'
			optional :page, type: String, desc: '页码'
		end
		get 'knowledges' do
			if params[:user_id].nil? 
				{code: "400", msg: "参数错误"}
			else
				if params[:page].nil?
					page = 1
				else
					page = params[:page]
				end
				knowledges = Knowledge.where(category_id: params[:category_id]).paginate(:page => page).order('created_at DESC')
				knowledge_list = []
				knowledges.each do |kl|
					user_knowledge = UserKnowledge.where(user_id: params[:user_id], knowledge_id: kl.id)
					if user_knowledge.empty?
						collection = 0
					else
						collection = 1
					end
					knowledge = Hash.new
					knowledge["id"] = kl.id
					knowledge["knowledge_tile"] = kl.knowledge_tile
					knowledge["file_type"] = kl.file_type
					knowledge["knowledge_url"] = kl.knowledge_url
					knowledge["collection"] = collection
					knowledge_list << knowledge
				end
				count = knowledges.size
				next_page = params[:page].to_i + 1
				{
					code: "0", 
					msg: "OK",
					knowledge_list: knowledge_list,
					count: count,
					next_page: next_page
				}
			end
		end

		#### 查询我收藏的知识 ####
		desc '查询我收藏的知识条目'
		params do
			optional :user_id, type: String, desc: '用户名'
			optional :page, type: String, desc: '页码'
		end
		get 'my_knowledges' do
			if params[:user_id].nil? 
				{code: "400", msg: "参数错误"}
			else
				if params[:page].nil?
					page = 1
				else
					page = params[:page]
				end
				user_knowledge = UserKnowledge.where(user_id: params[:user_id]).paginate(:page => page).order('created_at DESC')
				knowledge_list = []
				user_knowledge.each do |uk|
					knowledge = Knowledge.find(uk.knowledge_id)
					knowledge_info = Hash.new
					knowledge_info["id"] = knowledge.id
					knowledge_info["knowledge_tile"] = knowledge.knowledge_tile
					knowledge_info["file_type"] = knowledge.file_type
					knowledge_info["knowledge_url"] = knowledge.knowledge_url
					knowledge_info["collection"] = 1
					knowledge_list << knowledge_info
				end
				count = knowledge_list.size
				next_page = params[:page].to_i + 1
				{
					code: "0", 
					msg: "OK",
					knowledge_list: knowledge_list,
					count: count,
					next_page: next_page
				}
			end
		end
		#### 收藏知识 ####
		desc '收藏知识'
		params do
			optional :user_id, type: String, desc: '用户名'
			optional :knowledge_id, type: String, desc: '知识ID'
		end
		post 'collect_knowledge' do
			if params[:user_id].nil? || params[:knowledge_id].nil? 
				{code: "400", msg: "参数错误"}
			else
				user_knowledge = UserKnowledge.new
				user_knowledge.user_id = params[:user_id]
				user_knowledge.knowledge_id = params[:knowledge_id]
				user_knowledge.save
				{
					code: "0", 
					msg: "OK",
					collection: 1
				}
			end
		end

		desc '查询证书类目'
		params do
			optional :certificate_name, type: String, desc: '证书名字'
		end
		get 'certificate_types' do
			if  params[:certificate_name].nil?
				{code: "400", msg: "参数错误"}	
			else
				certificate_types = CertificateType.where("certificate_name LIKE ?", "%"+params[:certificate_name]+"%")
				certificate_type_list = []
				certificate_types.each do |ct|
					certificate_type_info = Hash.new 
					certificate_type_info["certificate_name"] = ct.certificate_name
					certificate_type_info["id"] = ct.id
					certificate_type_list << certificate_type_info
				end
				next_page = params[:page].to_i + 1
				{
					code: "0", 
					msg: "OK",
					certificate_type_list: certificate_type_list
				}
			end
		end

		desc '上传证书'
		params do
			optional :user_id, type: String, desc: '用户名'
			optional :file_url, type: String, desc: '证书地址' 
			optional :certificate_type, type: String, desc: '证书类型'
		end
		post 'upload_certificate' do
			if  params[:user_id].nil? || params[:file_url].nil? || params[:certificate_type].nil? 
				{code: "400", msg: "参数错误"}	
			else
				certificate = UserCertificate.new
				certificate.file_type_id = params[:certificate_type]
				certificate.file_url = params[:file_url]
				certificate.user_id = params[:user_id]
				certificate.save			
				{
					code: "0", 
					msg: "OK"
				}
			end
		end
		desc '查询个人证书列表'
		params do
			optional :user_id, type: String, desc: '用户名'
			optional :page, type: String, desc: '页码'
		end
		get 'certificates' do
			if  params[:user_id].nil? 
				{code: "400", msg: "参数错误"}	
			else
				if params[:page].nil?
					page = 1
				else
					page = params[:page]
				end
				certificates = UserCertificate.where(user_id: params[:user_id]).paginate(:page => page, :per_page => 9999).order('created_at DESC')
				certificate_list = []
				certificates.each do |cert|
					certificate_type = CertificateType.find(cert.file_type_id)
					certificate_info = Hash.new 
					certificate_info["certificate_type"] = certificate_type.certificate_name
					certificate_info["file_url"] = cert.file_url
					certificate_info["approval_status"] = cert.approval_status 
					certificate_list << certificate_info
				end
				count = certificates.size
				next_page = params[:page].to_i + 1
				{
					code: "0", 
					msg: "OK",
					certificates_list: certificate_list
				}
			end
		end
	end
end